#-------------------------------------------------------------------------------
# Name:        Ship
# Purpose:
#
# Author:      Michael Blackwood
#
# Created:     29/10/2013
# Copyright:   (c) Monxcleyr Productions 2013
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import pygame, logging
import base
from .. import tile

class Ship(base.Base):

  """ Ship Class"""

  def __init__(self, objtype, name, attributes, imagehandler):
    base.Base.__init__(self, objtype, name, attributes, imagehandler)
    self.ancestors.append('ship')

    self.loadCollisions()


  def getMask(self):

    mask = pygame.mask.from_surface(self.imagehandler.getSprite(self.attributes['sprite']), 0)
    return mask


  def loadCollisions(self):

    """ Loads collisions from .png """

    baseimage = self.imagehandler.getSprite(self.attributes['collision']).copy()
    colorlist = []

    for row in range(0,baseimage.get_height()):
      for column in range(0,baseimage.get_width()):
        temp = baseimage.get_at((column, row))
        temp = ((column, row), (temp[0],temp[1],temp[2]))
        colorlist.append(temp)


    tilelist = []
    for i in range(0, len(colorlist)):
      if colorlist[i][1] == (0,0,0):
        tilelist.append(((colorlist[i][0][0] * 16), (colorlist[i][0][1] * 16)))

    self.attributes['tiles'] = []

    for coord in tilelist:

      self.attributes['tiles'].append(tile.Tile(coord, (16,16), self))


    logging.info("Collisions loaded for %s", self.name)


  def loadBlitGrid(self):

    """ Create a list of rects for drawing the ship with later """

    size = self.imagehandler.getSprite(self.attributes['sprite'])



  def draw(self, surface, offset, common):

    if self.attributes['visable']:
      sprite = self.imagehandler.getSprite(self.attributes['sprite'])
      pos = self.attributes['pos']
      pos = (pos[0] + offset[0], pos[1] + offset[1])

      surface.blit(sprite, pos)


